package com.palm.hubo2o.util;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dest1 on 2015/10/22.
 * 双色球 选号辅助工具类
 */
public class SSQStakeBallUtils {

    public Map<String, String> redBallsMap = null;//红球
    public Map<String, String> blueBallsMap = null;//蓝球
    public Map<String, String> dmBallsMap = null;//胆码
    public Map<String, String> tmBallsMap = null;//拖码


    public SSQStakeBallUtils() {
        //普通
        redBallsMap = new HashMap<String, String>();
        blueBallsMap = new HashMap<String, String>();
        //胆托
        dmBallsMap = new HashMap<String, String>();
        tmBallsMap = new HashMap<String, String>();
    }


    /**
     * 往map中添加选球方法
     */
    public void addBall(Map<String, String> gameTypeMap, String ballNum) {

        if (gameTypeMap != null) {

            if (gameTypeMap.get(ballNum) != null) {
                //取消
                gameTypeMap.remove(ballNum);
            } else {
                //添加
                gameTypeMap.put(ballNum, ballNum);
            }
        }
    }

    /***
     * 添加拖码选球
     **/
    public void addBall(String redBall, String blueBall, String dmBallNum, String tmBllNum) {

        if (redBall != null) {
            addPTBall(redBallsMap, redBall);
        } else if (blueBall != null) {
            addPTBall(blueBallsMap, blueBall);
        } else if (dmBallNum != null) {
            addPTBall(dmBallsMap, dmBallNum);
        } else if (tmBllNum != null) {
            addPTBall(tmBallsMap, tmBllNum);
        }

    }


    /**
     * 添加普通选球
     **/
    public void addPTBall(Map<String, String> gameTypeMap, String ballNum) {
        addBall(gameTypeMap, ballNum);
    }


    /**
     * 得到选号数组
     */

    public String[] getBalls(Map<String, String> ballsMap) {

        List<String> ballList = new ArrayList<>();
        for (String key : ballsMap.keySet()) {
            ballList.add(key.toString());
        }
        return (String[]) ballList.toArray(new String[ballList.size()]);
    }

    /***
     * 得到红球选号数组
     **/
    public String[] getRedStakeBalls() {
        String[] balls = getBalls(redBallsMap);
        Arrays.sort(balls);
        return balls;
    }


    /***
     * 得到篮球选号数组
     **/
    public String[] getBlueStakeBalls() {
        String[] balls = getBalls(blueBallsMap);
        Arrays.sort(balls);
        return balls;
    }


    /***
     * 得到胆码选号数组
     **/
    public String[] getDMStakeBalls() {
        String[] balls = getBalls(dmBallsMap);
        Arrays.sort(balls);
        return balls;
    }

    /***
     * 得到胆码选号数组
     **/
    public String[] getTMStakeBalls() {
        String[] balls = getBalls(tmBallsMap);
        Arrays.sort(balls);
        return balls;
    }


    /**
     * 普通注数
     *
     * @return
     */
    public long getStakeNote() {
        String[] redBall = getBalls(redBallsMap);
        String[] blueBall = getBalls(blueBallsMap);
        int rednum = redBall.length;
        int bluenum = blueBall.length;

        if (rednum < 6 || bluenum == 0) {
            return 0;
        }
        long count = 1;
        for (int i = 7; i <= rednum; i++) {
            count = count * i;
        }
        for (int i = 2; i <= rednum - 6; i++) {
            count = count / i;
        }
        count = count * bluenum;
        return count;
    }


    public long getStakeNote(String[] redNum,String[] blueNum) {

        int rednum = redNum.length;
        int bluenum = blueNum.length;

        if (rednum < 6 || bluenum == 0) {
            return 0;
        }
        long count = 1;
        for (int i = 7; i <= rednum; i++) {
            count = count * i;
        }
        for (int i = 2; i <= rednum - 6; i++) {
            count = count / i;
        }
        count = count * bluenum;
        return count;
    }


    /***
     * 胆托注数计算
     **/
    public int getDTNote() {
        String[] dmBalls = getBalls(dmBallsMap);
        String[] tmBalls = getBalls(tmBallsMap);
        String[] blueBalls = getBalls(blueBallsMap);
        int danMaNum = dmBalls.length;
        int tuoMaNum = tmBalls.length;
        int blueNum = blueBalls.length;

        if (danMaNum == 0 || tuoMaNum == 0 || blueNum == 0) {
            return 0;
        }
        if (danMaNum > 5)
            return 0;
        if (tuoMaNum > 32)
            return 0;
        if (danMaNum + tuoMaNum < 7)
            return 0;
        if (blueNum == 0)
            return 0;
        int tempNum = 1;
        tempNum = comp(6 - danMaNum, tuoMaNum);
        tempNum = tempNum * blueNum;
        return tempNum;
    }

    private static int comp(int head, int foot) {
        int c = 1;
        for (int i = foot - head + 1; i <= foot; i++) {
            c = c * i;
        }
        for (int i = 2; i <= head; i++) {
            c = c / i;
        }
        return c;
    }


    /**
     * 清除原有map里边的值
     **/

    public void clearMap() {

        if (redBallsMap != null) {
            redBallsMap.clear();
        }
        if (blueBallsMap != null) {
            blueBallsMap.clear();
        }
        if (dmBallsMap != null) {
            dmBallsMap.clear();
        }
        if (tmBallsMap != null) {
            tmBallsMap.clear();
        }
    }
}
